import 'dart:async';
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:nft_shop/market/model/models.dart';
import 'package:nft_shop/my/view/album_hint.dart';
import 'package:nft_shop/res/global.dart';
import 'package:nft_shop/util/image_util.dart';
import 'package:nft_shop/util/other_utils.dart';
import 'package:nft_shop/widgets/load_image.dart';
import 'package:nft_shop/widgets/my_button.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:share/share.dart';
import 'dart:ui' as ui;

typedef MyCallBack = Function(String imagePath);

class MarketShareWidget extends StatefulWidget {
  GoodsInfo? info;

  MarketShareWidget({Key? key, this.info}) : super(key: key);

  @override
  _MarketShareWidgetState createState() => _MarketShareWidgetState();
}

class _MarketShareWidgetState extends State<MarketShareWidget> {
  GlobalKey _globalKey = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return RepaintBoundary(
      key: _globalKey,
      child: Container(
        decoration: BoxDecoration(
            color: Global.kTheme.colorBackground1,
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(16),
              topRight: Radius.circular(16),
            )),
        height: 203,
        padding: EdgeInsets.only(top: 30, left: 30, right: 30),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              "点击微信或QQ自动生成邀请链接",
              style: TextStyle(
                  fontSize: 14,
                  fontFamily: Global.mySemiboldFont,
                  color: Global.kTheme.textPrimary3),
            ),
            SizedBox(
              height: 30,
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                shareWidgetItem("share_1", "微信", onPressed: () {
                  Get.back();
                  Share.share(Global.AppHost + "download",
                      subject: Global.AppName);
                }),
                shareWidgetItem("share_2", "QQ", onPressed: () {
                  Get.back();
                  Share.share(Global.AppHost + "download",
                      subject: Global.AppName);
                }),
                shareWidgetItem("share_3", "复制链接", onPressed: () {
                  Get.back();
                  ClipboardData data =
                      new ClipboardData(text: Global.AppHost + "download");
                  Clipboard.setData(data);
                  EasyLoading.showToast("复制成功");
                }),
                shareWidgetItem("share_4", "生成海报", onPressed: () async {
                  Get.back();
                  Utils.showElasticDialog<void>(
                    context: context,
                    barrierDismissible: false,
                    builder: (BuildContext context) {
                      return posterWidget();
                    },
                  );
                }),
              ],
            )
          ],
        ),
      ),
    );
  }

  shareWidgetItem(String image, String title, {VoidCallback? onPressed}) {
    return InkWell(
      onTap: onPressed,
      child: Column(
        children: [
          LoadAssetImage(
            image,
            width: 44,
            height: 44,
          ),
          SizedBox(
            height: 10,
          ),
          Text(
            title,
            style: TextStyle(
              fontSize: 14,
              color: Global.kTheme.textPrimary3,
              fontFamily: Global.mySemiboldFont,
            ),
          )
        ],
      ),
    );
  }

  Widget posterWidget() {
    print("图片地址---${widget.info!.info!.thumb}");
    Widget content = Container(
      padding: EdgeInsets.symmetric(horizontal: 30),
      child: Stack(
        children: [
          Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              RepaintBoundary(
                key: _globalKey,
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(8),
                  child: Container(
                    color: Colors.transparent,
                    child: Stack(
                      children: [
                        Container(
                          width: Get.width - 60,
                          height: (Get.width - 60) + 80,
                          color: Colors.white,
                        ),
                        LoadImage(
                          widget.info!.info!.thumb ?? "",
                          width: Get.width - 60,
                          height: Get.width - 60,
                          fit: BoxFit.fitHeight,
                        ),
                        Positioned(
                          bottom: 0,
                          left: 0,
                          right: 0,
                          child: Container(
                            height: 93,
                            padding: EdgeInsets.symmetric(
                                horizontal: 15, vertical: 10),
                            decoration: BoxDecoration(
                              color: Global.kTheme.colorBackground1,
                              borderRadius: BorderRadius.circular(8),
                            ),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                QrImage(
                                  data: Global.AppHost + "download",
                                  padding: EdgeInsets.all(3),
                                ),
                                SizedBox(
                                  width: 7,
                                ),
                                Column(
                                  mainAxisSize: MainAxisSize.min,
                                  crossAxisAlignment: CrossAxisAlignment.start,
                                  children: [
                                    Text(
                                      widget.info!.info!.title ?? "",
                                      style: TextStyle(
                                          fontSize: 14,
                                          fontFamily: Global.mySemiboldFont,
                                          color: Global.kTheme.textPrimary4),
                                    ),
                                    SizedBox(
                                      height: 3,
                                    ),
                                    Text(
                                      "神秘的数字藏品,等你来收藏",
                                      style: TextStyle(
                                          fontSize: 12,
                                          fontFamily: Global.mySemiboldFont,
                                          color: Global.kTheme.textPrimary3),
                                    ),
                                  ],
                                )
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ),
              ),
              SizedBox(
                height: 15,
              ),
              Container(
                height: 40,
                decoration: BoxDecoration(
                  color: Global.kTheme.colorBackground3,
                  borderRadius: BorderRadius.circular(4),
                ),
                child: MyButton(
                  minHeight: 40,
                  onPressed: () async {
                    RenderRepaintBoundary boundary = _globalKey.currentContext!
                        .findRenderObject() as RenderRepaintBoundary;
                    ui.Image image = await boundary.toImage();
                    ByteData? byteData = await (image.toByteData(
                        format: ui.ImageByteFormat.png) as FutureOr<ByteData?>);
                    if (byteData != null) {
                      AppUtil.saveByteImage(byteData.buffer.asUint8List());
                      Navigator.of(Get.context!)
                        ..pop()
                        ..pop();
                    } else {
                      EasyLoading.showToast('失败');
                    }
                  },
                  text: "保存",
                  fontSize: 16,
                  textColor: Colors.white,
                  backgroundColor: Global.kTheme.colorBackground3,
                ),
              ),
            ],
          ),
          Positioned(
              top: 0,
              right: 0,
              child: IconButton(
                icon: Icon(
                  Icons.close,
                  size: 20,
                ),
                onPressed: () {
                  Get.back();
                },
              )),
        ],
      ),
    );

    final Widget body = MediaQuery.removeViewInsets(
      removeLeft: true,
      removeTop: true,
      removeRight: true,
      removeBottom: true,
      context: context,
      child: Center(child: content),
    );

    return Scaffold(
      backgroundColor: Colors.transparent, //透明类型
      body: body,
    );
  }
}
